package com.example.onlineeducation.adapter

import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.example.onlineeducation.R
import com.example.onlineeducation.data.constant.ProblemType
import com.example.onlineeducation.data.entity.OptionEntity
import com.example.onlineeducation.data.entity.ProblemEntity
import com.example.onlineeducation.databinding.ProblemItemBinding
import com.example.onlineeducation.databinding.ProblemItemItemBinding
import com.example.onlineeducation.utils.YcSpannable
import com.yc.jetpacklib.extension.ycIsNotEmpty
import com.yc.jetpacklib.extension.ycLoadImageRes
import com.yc.jetpacklib.extension.ycSetTextColorRes
import com.yc.jetpacklib.init.YcJetpack
import com.yc.jetpacklib.recycleView.YcDecoration
import com.yc.jetpacklib.recycleView.YcViewHolder

/**
 * Creator: yc
 * Date: 2022/4/22 15:05
 * UseDes:
 */
class ProblemAdapter : RecyclerView.Adapter<YcViewHolder<ProblemItemBinding>>() {
    private var mData: MutableList<ProblemEntity> = mutableListOf()
    private var mInternalAdapter: MutableList<InternalAdapter> = mutableListOf()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): YcViewHolder<ProblemItemBinding> {
        return YcViewHolder(ProblemItemBinding.inflate(LayoutInflater.from(parent.context), parent, false))
    }

    override fun onBindViewHolder(holder: YcViewHolder<ProblemItemBinding>, position: Int) {
        try {
            holder.viewBinding.apply {
                val item = mData[position]
                while (mInternalAdapter.size <= position) {
                    mInternalAdapter.add(InternalAdapter())
                }
                rvProblemOption.adapter = mInternalAdapter[position]
                rvProblemOption.addItemDecoration(YcDecoration(20))
                mInternalAdapter[position].addAll(item.optionList, item.problemType)
                tvProblemContent.text = item.title
                YcSpannable.setText(
                    tvProblemContent,
                    item.title,
                    "(${ProblemType.getTypeName(item.problemType)})",
                    YcJetpack.mInstance.getResources().getColor(R.color.problem_content_type)
                )

            }
        } catch (e: Exception) {
            Log.e("ycEvery", "onBindViewHolder爆炸啦")
            e.printStackTrace()
        }
    }

    override fun getItemCount(): Int {
        return mData.size
    }

    /**
     * 检测此题是否有选中
     * @param position Int
     * @return Boolean
     */
    fun checkProblemSelect(position: Int): Boolean {
        var isHasSelect = false
        mData[position].optionList?.apply {
            for (item in this) {
                if (item.isSelect) {
                    isHasSelect = true
                    break
                }
            }
        }
        return isHasSelect
    }

    fun addAll(newData: List<ProblemEntity>?) {
        mData.clear()
        if (newData.ycIsNotEmpty())
            mData.addAll(newData!!)
        notifyItemRangeChanged(0, mData.size)
    }

    fun reset() {
        mData.forEach {
            it.optionList?.forEach {
                it.isSelect = false
            }
        }
        notifyItemRangeChanged(0, mData.size)
    }

    fun getData(): List<ProblemEntity> = mData

    internal class InternalAdapter : RecyclerView.Adapter<YcViewHolder<ProblemItemItemBinding>>() {
        private var mInternalData: MutableList<OptionEntity> = mutableListOf()
        private var mSingleSelectPosition: Int? = null

        @ProblemType.Type
        private var mProblemType: Int = ProblemType.SINGLE
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): YcViewHolder<ProblemItemItemBinding> {
            return YcViewHolder(ProblemItemItemBinding.inflate(LayoutInflater.from(parent.context), parent, false))
        }

        override fun onBindViewHolder(holder: YcViewHolder<ProblemItemItemBinding>, position: Int) {
            try {
                holder.viewBinding.apply {
                    val internalItem = mInternalData[position]
                    tvOptionContent.text = internalItem.context
                    tvOptionContent.ycSetTextColorRes(ProblemType.getTextColor(internalItem.isSelect))
                    ivOptionSelect.ycLoadImageRes(ProblemType.getDrawable(mProblemType, internalItem.isSelect))
                    clBg.setBackgroundResource(ProblemType.getBgColor(internalItem.isSelect))
                    root.setOnClickListener {
                        val bindingPosition = holder.bindingAdapterPosition
                        val item = mInternalData[bindingPosition]
                        when (mProblemType) {
                            ProblemType.SINGLE -> {
                                if (mSingleSelectPosition != bindingPosition) {
                                    if (mSingleSelectPosition != null) {
                                        mInternalData[mSingleSelectPosition!!].isSelect = false
                                        notifyItemChanged(mSingleSelectPosition!!)
                                    }
                                    mSingleSelectPosition = bindingPosition
                                    item.isSelect = true
                                    notifyItemChanged(mSingleSelectPosition!!)
                                }
                            }
                            ProblemType.MULTIPLE -> {
                                item.isSelect = !item.isSelect
                                notifyItemChanged(bindingPosition)
                            }
                            else -> {
                                if (mSingleSelectPosition != bindingPosition) {
                                    if (mSingleSelectPosition != null) {
                                        mInternalData[mSingleSelectPosition!!].isSelect = false
                                        notifyItemChanged(mSingleSelectPosition!!)
                                    }
                                    mSingleSelectPosition = bindingPosition
                                    item.isSelect = true
                                    notifyItemChanged(mSingleSelectPosition!!)
                                }
                            }
                        }
                    }
                }
            } catch (e: Exception) {
                Log.e("ycEvery", "onBindViewHolder爆炸啦")
                e.printStackTrace()
            }
        }

        override fun getItemCount(): Int {
            return mInternalData.size
        }

        fun addAll(newData: List<OptionEntity>?, @ProblemType.Type problemType: Int) {
            mSingleSelectPosition = null
            mProblemType = problemType
            mInternalData.clear()
            if (newData.ycIsNotEmpty())
                mInternalData.addAll(newData!!)
            notifyItemRangeChanged(0, mInternalData.size)
        }
    }
}